package day04;

import java.util.Date;
/*
@Author: 王少杰
@Date: 2025/1/3
@Time: 15:02
@FileName: Demo07.java
@描述:
字符串:
     String:不可变类型
     StringBuilder、StringBuffer可变类型  操作大量字符串时效率更高。
*/
public class Demo07 {
    public static void main(String[] args) {
        //1,2,3,4,... + 10000

        Date date = new Date();
        long time = date.getTime();
        String a = "";
        /*
        "1"
        "2"
        "1","2"
        没拼接一个字符，创建一个临时对象，过程中会生成很多临时对象，占用内存
         */
        for (int i = 1; i < 50000; i++) {
            a += i + ",";
        }
//        System.out.println(a);
        System.out.println("字符串的长度为:" + a.length());
        Date date1 = new Date();
        long time1 = date1.getTime();
        //10000 75ms
        //50000 937ms
        System.out.println("拼接耗时:" + (time1 - time) + "ms");

        Date date2 = new Date();
        long time2 = date2.getTime();
//        StringBuilder线程不安全。多线程同时操作一个字符串
//        StringBuffer线程安全
//        StringBuilder stringBuilder = new StringBuilder();//创建对象
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 1; i < 50000; i++) {
            stringBuffer.append(i).append(",");
        }
        System.out.println("字符串的长度为:" + a.length());
        Date date3 = new Date();
        long time3 = date3.getTime();
        System.out.println("拼接耗时:" + (time3 - time2) + "ms");

        //append
        //insert
        //delete
        //reverse

        StringBuffer stringBuffer1 = new StringBuffer("HelloWorld");
        stringBuffer1.insert(0,"***");
        System.out.println(stringBuffer1);
        stringBuffer1.delete(3,8);//包含3，不包含8
        System.out.println(stringBuffer1);
        stringBuffer1.reverse();
        System.out.println(stringBuffer1);
        stringBuffer1.deleteCharAt(0);
        System.out.println(stringBuffer1);

    }
}